<?php
    include_once('session.php');
        
    //viene a modificar?
    if(isset($_GET['i'])){
        $id_empresa = $_GET['i'];
        require_once('php/class/empresas.class.php');
        $oEmpresa = new empresa();
        $oEmpresa->search($id_empresa);
        $nombre = $oEmpresa->NOMBRE;
        $contacto = $oEmpresa->CONTACTO;
        $telefono = $oEmpresa->TELEFONO;
        $mail = $oEmpresa->MAIL;
        
        $accion="MODIFY";
        
    }else{
       //variables
        $nombre = "";
        $telefono = "";
        $contacto = "";
        $mail = "";
        $accion = "ADD";
        $id_empresa = "";
    }
?>
<html>
    <?php include('include.header_admin.html'); ?>
    <body class="bodyadmin">
        <div id="empresa" align="center">
            
            <form id="frm_bp" name="frm_bp" method="post">
                <input type="hidden" id="ACCION" name="ACCION" value="<?php echo $accion;?>">
                <input type="hidden" id="id" name="id" value="<?php echo $id_empresa;?>">
                <table class="marco">
                <tr>
                <td height="10%">
                <?php include('menu.php'); ?>
                </td>
                </tr>
                    <tr height="5%">
			<td align="center" valign="button">
			    Registro de empresas
			</td>
		    </tr>
                    <tr>
                        <td align="center" valign="middle" >
                            <div id="DATO_HIDE" style="display:none" style="width:90%">
                                <div id="DATO"></div>
                            </div>
                <table class="formulario">
                <tr>
                    <td>Raz&oacute;n social</td>
                    <td><input type="text" id="nombre" name="nombre" class="validate['required','nodigit','length[1,20]']" value="<?php echo $nombre;?>"></td>
                </tr>
                <tr>
                    <td>Nombre de un contacto</td>
                    <td><input type="text" id="contacto" name="contacto" class="validate['required','nodigit','length[1,20]']" value="<?php echo $contacto;?>"></td>
                </tr>
                <tr>
                    <td>Tel&eacute;fono</td>
                    <td><input type="text" id="telefono" name="telefono" class="validate['required','phone']" value="<?php echo $telefono;?>"></td>
                </tr>
                <tr>
                    <td>Mail</td>
                    <td><input type="text" id="mail" name="mail" class="validate['required','email']" value="<?php echo $mail;?>"></td>
                </tr>
                <tr>
                    <td align="right">
                        <input type="button" value="Cancel" onclick="javascript:window.location='list_empresas.php';">
                    </td>
                    <td align="left">
                        <input type="submit" id="submit" name="submit" value="Guardar">
                    </td>
                </tr>
            </table>
                        </td>
                    </tr>
                    <tr>
			<td height="10%" valign="bottom" align="center">
			<?php include('footer.php'); ?>
			</td>
			</tr>
                </table>
            </form>
            
        </div>
    </body>
    <?php include('include.footer_admin.html'); ?>
    <script>
    ///////////////////////////////
    var myCheck;
	myCheck = new FormCheck('frm_bp', {
	  submit:false,
		display:{
			errorsLocation : 1,
			indicateErrors : 2,
			keepFocusOnError : 0,
			closeTipsButton : 1,
			checkValueIfEmpty:1,                
			showErrors : 1
		}
	});		 
 
    $('frm_bp').addEvent('submit',function(ev){
        var IsValid;
       IsValid=myCheck.isFormValid();                                                                          
        if (IsValid){ 
            
             Ejecutor();
        }    
    });
    ///////////////////////////////

    function Ejecutor(){        
        window.addEvent('domready', function() {      	       
    		var req = new Request.JSON({
                method: 'POST',
                url: 'ajax/ajax_empresa.php', 
                data: $('frm_bp'),                
                noCache:true,                     
                onRequest: function(){
                    $('submit').disabled = true;
                    $('DATO_HIDE').show();
                    $('DATO').innerHTML = 'Procesando ...'; 
                },
                onComplete: function(){
                    setTimeout(function(){
                        $('DATO_HIDE').fade('out').get('tween').chain(function() {
                            $('DATO_HIDE').hide();
                            $('DATO_HIDE').fade('in');                                                         
                        });
                    },2000);                                                                                                                                                                                                              
                },             
                onSuccess: function(response){
                    switch(parseInt(response.RESULT)){
                        case 0:
                            $('DATO').innerHTML = "Operaci&oacute;n exitosa";  
                            window.location='list_empresas.php';
                            break;
                        case 1:
                            $('DATO').innerHTML = response.ERROR;
                            $('submit').disabled = false;
                            break;
                    }
                }
            }).get();        
       });
    }
    </script>
</html>
